package io.xxx.mario.job;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

@Service
public class JobService {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private TaskScheduler taskScheduler;

    public Long addJob(Date date) throws SchedulerException {
        long jobId = IdWorker.getId();
//        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
//                .withIdentity(jobId + "", "task")
//                .withDescription("消息推送任务")
//                .build();
//        SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow().withRepeatCount(1);
//        Trigger trigger = TriggerBuilder
//                .newTrigger()
//                .withIdentity(jobId + "", "task")
//                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * ? * * *"))
////                .startAt(date)
////                .endAt(new Date(date.getTime() + 1000 * 10))
//                .build();
//        scheduler.scheduleJob(jobDetail, trigger);

        ScheduledFuture<?> future = taskScheduler.schedule(() -> System.out.println("123"), new CronTrigger("0/2 * * * * *"));

        return jobId;
    }

    public boolean interruptJob(Long jobId) throws SchedulerException {
        JobKey jobKey = new JobKey(jobId.toString(), "task");
        return scheduler.interrupt(jobKey);
    }
}
